# cython: language_level=3
#!/usr/bin/python
# -- coding:utf8 --
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if __name__ == '__main__':
    import model_vn.util_log as util_log
    import model_vn.cal_app_features as app
    import model_vn.cal_userinfo_features as userinfo
    import model_vn.cal_sms_features as sms
    # import model_vn.cal_device_features as device
    import model_vn.model_predict as md
else:
    import app.model_vn.util_log as util_log
    import app.model_vn.cal_app_features as app
    import app.model_vn.cal_userinfo_features as userinfo
    import app.model_vn.cal_sms_features as sms
    # import app.model_vn.cal_device_features as device
    import app.model_vn.model_predict as md
import time
import warnings

warnings.filterwarnings('ignore')


# 把短信内容转化成可读取的内容格式
def decode_str(x):
    arr = x.split(',')
    content = ""
    if len(arr) > 0:
        try:
            for v in arr:
                vDecode = chr(int(v, 16))
                content = content + vDecode
        except UnicodeEncodeError:
            content = content.encode('utf-8', errors='ignore').decode('utf-8', errors='ignore')
            # 或者使用 content = content.encode('latin-1', errors='ignore').decode('latin-1', errors='ignore')
        except:
           # print(v)
            content = content + v
    else:
        content = ""
    return content

def get_free_features(phone_sub, ApplyNO):
    try:
        # 1 用户基本信息特征集
        start = time.time()
        userinfo_features = userinfo.get_userinfo_freatures(ApplyNO)
        free_features = userinfo_features
        end = time.time()
        running_time = end - start
        print('1:用户基本信息特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 2 APP特征集
        start = time.time()
        app_features2 = app.get_free_app_features(phone_sub, ApplyNO)
        free_features.update(app_features2)
        end = time.time()
        running_time = end - start
        print('2:APP特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 3 SMS特征集
        start = time.time()
        sms_features = sms.get_free_sms_features(phone_sub, ApplyNO)
        free_features.update(sms_features)
        # sms_features1 = sms.get_free_sms_features_v1(phone_sub, ApplyNO)
        # free_features.update(sms_features1)
        end = time.time()
        running_time = end - start
        print('3 SMS特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 4 设备特征集
        # start = time.time()
        # device_features = device.get_device_freatures(ApplyNO)
        # free_features.update(device_features)
        # end = time.time()
        # running_time = end - start
        # print('4Device特征模块计算耗费时间 : %.5f 秒' % running_time)

        # 5 用户评分模块
        start = time.time()
        model_score_features = md.get_user_model_score_main(free_features)
        free_features.update(model_score_features)
        end = time.time()
        running_time = end - start
        print('5 用户评分模块耗费时间 : %.5f 秒' % running_time)
        return free_features

    except Exception as e:
        util_log.get_logger().error(e)
        return {}


if __name__ == '__main__':
    # 新客V2版本测试用例
    phone_sub = 1
    ApplyNO = '1679732000463928'
    free_features = get_free_features(phone_sub, ApplyNO)

    print(free_features)
